package com.maxh.controller;

import com.maxh.annotation.PassToken;
import com.maxh.annotation.SuperAuthority;
import com.maxh.annotation.AdminLoginToken;
import com.maxh.annotation.TeacherLoginToken;
import com.maxh.pojo.Course;
import com.maxh.pojo.Information;
import com.maxh.service.CourseService;
import com.maxh.service.InformationService;
import com.maxh.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CourseController {
    @Autowired
    CourseService courseService;
    @Autowired
    InformationService informationService;

    /*管理员获取课程信息*/
    @AdminLoginToken
    @GetMapping("/training/course/content/list")
    public Object courseList(){
        List courseList = courseService.courseList();
        System.out.println("管理员获取课程信息："+courseList);
        if(courseList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"获取数据完成！");
        }else{
            return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"数据为空！");
        }
    }
    //  管理员添加课程信息
    @AdminLoginToken
    @PostMapping("/training/course/content/add")
    public Object addCourse(Course course) throws ParseException {
        if(course.getCourseNum()==null || course.getCourseName()==null || course.getCourseRoom()==null ||course.getCourseDate()==null ||
            course.getCourseWeek()==null || course.getCourseTime()==null || course.getCourseType()==null || course.getCourseDesc()==null ||
                course.getStopTime()==null || course.getTeacherId()==null || course.getAttributeId()==null || course.getPrice()==null
        ){
            return new ReturnError(StatusCode.RETURN_ERROR,"必填课程信息不能为空");
        }
        //判断课程编号是否为空
        Map<String, Object> mapNum = new HashMap<String,Object>();
        mapNum.put("reCourseNum",course.getCourseNum());
        List courseList = courseService.findCourse(mapNum);
        if(courseList.size()>0)  return new ReturnData(StatusCode.RETURN_ERROR ,"课程编号已存在，请重新输入！");

        Date dateTime = new ReturnUtil().dateTime();
        //课程使用状态
        course.setCourseStatus(1);
        course.setCreateTime(dateTime);
        //课程数据状态
        course.setStatus(1);
        Boolean isAddCourse = courseService.addCourseList(course);
        System.out.println("管理员添加课程信息："+isAddCourse);
        if(isAddCourse){
            Map<String, Object> map = new HashMap<String,Object>();
            System.out.println(map);
            List courseList1 = courseService.findCourse(map);
            return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList1 ,"添加成功！");
        }else{
            return new ReturnData(StatusCode.RETURN_ERROR ,"添加失败！");
        }
    }
    //根据课程号、课程名、讲师Id、讲师名、讲师编号或者id查询
//    @AdminLoginToken
    @PassToken
    @GetMapping("/training/course/content/search")
    public Object searchCourse(@RequestParam Map<String,Object> map){
        if(map==null)  return new ReturnError(StatusCode.RETURN_ERROR,"必填课程信息不能为空");
        List courseList = courseService.findCourse(map);
        System.out.println("根据课程号、课程名、讲师Id、讲师名、讲师编号或者id查询："+courseList);
        if(courseList.size()>0) return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"查询成功！");
        else return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"数据为空！");
    }
    //超级管理员删除课程信息
    @SuperAuthority
    @PostMapping("/training/course/content/delete")
    public Object deleteContent(@RequestParam Map<String,Object> map){
        if(map.get("id")==null)  return new ReturnError(StatusCode.RETURN_ERROR,"课程ID不能为空");
        Boolean isDelete = courseService.deleteContent(map.get("id")+"");
        System.out.println("超级管理员删除课程信息："+isDelete);
        if(isDelete){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,"删除成功！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"删除失败");
        }
    }
    //管理员修改课程信息
    @AdminLoginToken
    @PostMapping("/training/course/content/update")
    public Object updateCourse(Course course){
        if(course.getId() < 1 ) return new ReturnError(StatusCode.RETURN_ERROR,"课程ID不能为空");
        Boolean isUpdateCourse = courseService.updateCourseList(course);
        System.out.println("管理员修改课程信息："+isUpdateCourse);
        if(isUpdateCourse){
            Map<String, Object> map = new HashMap<String,Object>();
            map.put("id",course.getId());
            //查找修改的课程信息
            List courseList = courseService.findCourse(map);
            return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"修改成功！");
        }else{
            return new ReturnData(StatusCode.RETURN_ERROR ,"修改失败！");
        }
    }
    //  管理员修改课程状态
    @AdminLoginToken
    @PostMapping("/training/course/content/updateStatus")
    public Object updateCourseStatus(Course course) throws ParseException {
        if(course.getId() < 1 || course.getCourseStatus() < 0) return new ReturnError(StatusCode.RETURN_ERROR,"课程ID和课程状态不能为空");
        Date dateTime = new ReturnUtil().dateTime();
        course.setCreateTime(dateTime);
        Boolean isUpdateCourseStatus = courseService.updateCourseStatus(course);
        System.out.println("管理员修改课程信息状态："+isUpdateCourseStatus);
        if(isUpdateCourseStatus){
            return new ReturnData(StatusCode.REQUEST_SUCCESS ,"修改成功！");
        }else{
            return new ReturnData(StatusCode.RETURN_ERROR ,"修改失败！");
        }
    }

    /*讲师获取所有上架课程信息*/
    @TeacherLoginToken
    @GetMapping("/training/curriculum/list")
    public Object curriculumList(){
        List courseList = courseService.curriculumList();
        System.out.println("讲师获取所有上架课程信息："+courseList);
        if(courseList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"数据获取完成！");
        }else{
            return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"数据为空！");
        }
    }
    //讲师根据课程号、课程名或者id查询
    @TeacherLoginToken
    @GetMapping("/training/curriculum/search")
    public Object searchCurriculum(@RequestParam Map<String,Object> map){
        if(map==null)  return new ReturnError(StatusCode.RETURN_ERROR,"查询关键字不能为空");
        List courseList = courseService.searchCurriculum(map);
        System.out.println("根据关键字进行查询courseList"+courseList);
        if(courseList.size()>0) return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"查询成功！");
        else return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"数据为空！");
    }

    //讲师获取个人所有上架课程信息
    @TeacherLoginToken
    @GetMapping("/training/teacher/personal/course")
    public Object PersonalCourse(){
        String teacherId = TokenUtil.getTokenTeacherId();
        System.out.println(teacherId);
        List courseList = courseService.findTeacherCourse(teacherId);
        System.out.println("讲师获取个人所有上架课程信息："+courseList);
        if(courseList.size()>0) return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"数据获取完成！");
        else return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"数据为空！");
    }

    //学员根据课程id查询课程信息(没有用到)
    @GetMapping("/training/course/content/searchById")
    public Object searchCourseById(@RequestParam Map<String,Object> map){
        if(map.get("id")==null)  return new ReturnError(StatusCode.RETURN_ERROR,"课程Id不能为空");
        List courseList = courseService.findCourse(map);
        System.out.println("根据关键字进行查询courseList"+courseList);
        if(courseList.size()>0){
            String userId = TokenUtil.getTokenUserId();
            Information information=null;
            information.setUserId(Integer.parseInt(userId));
            int  isInformation = informationService.searchInformationList(information);
            courseList.add(isInformation);
            System.out.println(isInformation);
            return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"数据获取完成！");
        } else return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"数据为空！");
    }

    //学员根据课程号、课程名或者id查询
    @GetMapping("/training/student/curriculum/search")
    public Object studentSearchCurriculum(@RequestParam Map<String,Object> map){
        if(map==null)  return new ReturnError(StatusCode.RETURN_ERROR,"关键字不能为空");
        List courseList = courseService.searchCurriculum(map);
        System.out.println("学员根据课程号、课程名或者id查询："+courseList);
        if(courseList.size()>0) return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"数据获取完成！");
        else return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"数据为空！");
    }
    /*学员获取所有上架课程信息*/
    @GetMapping("/training/student/course/list")
    public Object CourseListByTime(){
        List courseList = courseService.courseListByTime();
        System.out.println("学员获取所有上架课程信息："+courseList);
        if(courseList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"数据获取完成！");
        }else{
            return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"数据为空！");
        }
    }
}
